package com.jxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxx.mapper.ItemDescMapper;
import com.jxx.mapper.ItemMapper;
import com.jxx.pojo.Item;
import com.jxx.pojo.ItemDesc;
import com.jxx.service.ItemService;
import com.jxx.vo.EasyUITable;
import com.jxx.vo.ItemDetails;
import com.jxx.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;

    /**
     * @author Zcc
     * @desc 商品分页
     * @param   page,rows
     * @return EasyUITable
     * @date 2020年11月27日08:26:51
     */
    @Override
    public EasyUITable findItemByPage(Integer page, Integer rows,Integer categoryId,String msg) {
        IPage<Item> iPage = new Page<>(page,rows);
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
         if (categoryId!=null){
            queryWrapper.orderByDesc("update_time")
                    .eq("category_id", categoryId);
        }else if (msg!=""){
            queryWrapper.like("item_title", msg);
            List<Item> searchList = itemMapper.selectList(queryWrapper);
        }else {
             queryWrapper.orderByDesc("update_time");
         }
        iPage = itemMapper.selectPage(iPage,queryWrapper);
        return new EasyUITable(iPage.getTotal(),iPage.getRecords());
    }

    @Override
    public SysResult addItem(Item item, ItemDesc itemDesc) {
        itemMapper.insert(item);
        itemDesc.setItemId(item.getItemId());
        itemDescMapper.insert(itemDesc);
        return SysResult.success();
    }

    @Override
    public ItemDetails findItem(Integer itemId) {
        return itemMapper.selectByItemId(itemId);
    }
}
